<template>
  <div>
    <el-form
      :model="postForm"
      ref="postForm"
      :rules="rules"
      label-width="200px"
      label-suffix=":"
    >
      <sticky :z-index="10" :class-name="'sub-navbar ' + postForm.status">
        <el-button v-loading="loading" style="margin-left: 10px;" type="success" @click="submitForm">
          保存
        </el-button>
      </sticky>

      <div style="padding: 15px">
        <help />

        <el-row>
          <el-col :span="20">
            <el-form-item label="创业池比率">
              <el-input v-model.trim="postForm.start_up_pool"/>
              <span>百分比</span>
            </el-form-item>
            <el-form-item label="分红池比率" v-if="false">
              <el-input v-model.trim="postForm.dividend_pool"/>
              <span>利润的百分比</span>
            </el-form-item>
            <el-form-item label="成团人数">
              <el-input v-model.trim="postForm.quantity"/>
              <span>多少人成团</span>
            </el-form-item>
            <el-form-item label="中奖人数">
              <el-input v-model.trim="postForm.win_total"/>
              <span>中奖人数(得产品)</span>
            </el-form-item>
            <el-form-item label="不中返多少佣金">
              <el-input v-model.trim="postForm.no_win_commission"/>
              <span>不中返多少佣金</span>
            </el-form-item>
            <el-form-item label="升级大团长">
              <el-input v-model.trim="postForm.vip2_recommend_finish_group_total"/>
              <span>直推团长数量(已经成过团)</span>
            </el-form-item>
            <el-form-item label="成团奖比率">
              <el-input v-model.trim="postForm.vip2_recommend_finish_group_award"/>
              <span>成团(大团长), 利润百分比</span>
            </el-form-item>
            <el-form-item label="蜜卡分红" v-if="false">
              <el-input v-model.trim="postForm.card_total"/>
              <span>集齐多少张蜜卡, 可分红</span>
            </el-form-item>
            <el-form-item label="开团时长" v-if="false">
              <el-input v-model.trim="postForm.time"/>
              <span>默认结束时长, 单位:分钟 (1天 = 1440)</span>
            </el-form-item>
          </el-col>
        </el-row>
      </div>

    </el-form>
  </div>
</template>

<script>
import Sticky from '@/components/Sticky'
import api from '@/api'
import Help from './components/Help'
export default {
  name: 'MarketBasic',
  components: {
    Sticky,
    Help
  },
  data() {
    return {
      postForm: {
        status: 'draft'
      },
      rules: {
      },
      loading: false
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    async fetchData() {
      const { data } = await api.marketing.fetchGroup()
      this.postForm = Object.assign({}, data)
      this.postForm.status = 'publisher'
    },
    submitForm() {
      this.$refs.postForm.validate(validate => {
        if (validate) {
          if (+this.postForm.start_up_pool > 50) {
            return this.$message.warning('创业池比率过高')
          }

          if (+this.postForm.win_total > +this.postForm.quantity) {
            return this.$message.error('中奖人数不能大于成团人数')
          }

          if (+this.postForm.vip2_recommend_finish_group_total < 1) {
            return this.$message.warning('升级大团长条件错误')
          }

          if (+this.postForm.vip2_recommend_finish_group_award > 10) {
            return this.$message.warning('成团奖比例过高')
          }

          this.loading = true
          api.marketing.saveGroup({
            ...this.postForm
          }).then(response => {
            this.$notify({
              title: '成功',
              message: response.message || '保存成功',
              type: 'success',
              duration: 2000
            })
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        }
      })
    }
  }
}
</script>
